<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport"
          content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">

    <title>{$mach['merchant']}-快速付款</title>
    <script src="__STATIC_URL__/static/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="__STATIC_URL__/static/css/pay.css?id=201"/>
    <script type="text/javascript" src="__STATIC_URL__/static/js/keyboards.js?v=4"></script>
    <script>

        var sdata;
        var pay_catename = "支付宝支付";
        function getorder() {
            var id = {$id};
            $.ajax({
                type: "POST",
                url: "{:url('Huifupay/getorder2')}",

                cache: false,
                async: false,
                data: {
                    id: id
                },


                success: function (data) {
                    $('#orderid').val(data);

                }
            });

        }

        function ready(callback) {
            if (window.AlipayJSBridge) {
                callback && callback();
            } else {
                document.addEventListener('AlipayJSBridgeReady', callback, false);
            }
        }
        function tradePay(tradeNO) {

            ready(function(){

                // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
                AlipayJSBridge.call("tradePay", {
                    tradeNO: tradeNO

                }, function (data) {
                    if (9000 == data.resultCode) {
                        var $loadingToast = $('#loadingToast');
                        setTimeout(function () {
                            $loadingToast.fadeOut(100);
                        }, 100);

                        paysuccess();
                    }else{
                        var $loadingToast = $('#loadingToast');
                        setTimeout(function () {
                            $loadingToast.fadeOut(100);
                        }, 100);
                        $('#price').val("");
                        $("#paytoBtn").attr("onclick","sendpay();");
                        $('#paytoBtn').addClass('disabled');
                        $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                        $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span>'+pay_catename+'</span><b></b>');
                    }
                });
            });
        }
        function paysuccess() {
            var ordid = $('#orderid').val();
            $.ajax({
                type: "POST",
                url: "/Huifupay/payover/id/{$id}/orderid/" + ordid,

                cache: false,
                dataType: "json",
                async: false,
                data: {},

                success: function (data) {  var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    $('#price').val("");

                    if (data==0) {

                        window.location.replace("/Huifupay/aliorderinfo/id/{$id}/orderid/" + ordid);
                    } else {

                        alert('支付失败');
                    }
                    ////
                }


            });

        }
        function zf() {
            $("#paytoBtn").prop("onclick",null).off("click");

            getorder();
            var price = $('#price').val();
            if (price < 0.01) {
                alert('金额不能小于0.01元');
                $('#price').focus();
            } else if (price > 20000) {
                alert('超过最大支付限额');
                $('#price').focus();
            }

            else {
                $("#submit").removeClass("submit");
                $("#submit").addClass("submita");
                $('#submit').attr('onclick', '()');
                $('#submit').val('正在处理中...');

                $.ajax({
                    type: "POST",
                    url: "{:url('Huifupay/alipay_send',array('id'=>$id))}",

                    cache: false,
                    dataType: "json",
                    async: false,
                    data: $('#tab').serialize(),

                    success: function (res) {
                        console.log(res.info);
                        if (res.result_code === "FAIL") {
                            alert('网络超时请重新支付');
                            getorder();
                            $("#submit").removeClass("submita");
                            $("#submit").addClass("submit");
                            $('#submit').attr('onclick', 'zf()');
                            $('#submit').val('重新支付');
                            var $loadingToast = $('#loadingToast');
                            setTimeout(function () {
                                $loadingToast.fadeOut(100);
                            }, 100);
                            $('#price').val("");
                            $("#paytoBtn").attr("onclick","sendpay();");
                            $('#paytoBtn').addClass('disabled');
                            $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                            $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">支付宝支付</span><b></b>');

                        }else{
                            tradePay(res.info.tradeNO);
                        }

                    }
                });

            }
        }
    </script>
</head>
<body>

<div class="zt">
    <div class="zw"></div>
    <div style="width: 100%;" class="header">
        <div class="top">
            <div class="logo"><img src="{$mach['img']}"></div>
            <div class="mallinfo"><span >{$mach['merchant']}</span></div>
        </div>

        <div style="clear:both"></div>
    </div>
    <div class="zw"></div>
    <div class="cardexplain">

        <ul class="round">

            <li class="nob">
                <table>

                    <tr>
                        <td>
                            <form role="form" id="tab">
                                <div class="form-group">
                                    <span id="ti">金额(元)</span>
                                    <label for="name"><span class="span">￥</span></label>
                                    <input type="text" class="form-control" id="price" name="price"
                                           oninput="if(value.length>5)value=value.slice(0,5)" readonly="readonly">
                                    <input type="hidden" name="buyer_id" id="buyer_id" value="{$uid}"/>
                                    <input type="hidden" name="orderid" id="orderid" value=""/>
                                </div>
                            </form>
                        <td>
                    </tr>
                </table>


        </ul>
    </div>


    <!--<div class="foots"><span>支付完成后，如需退款请及时联系店员</span></div>-->

    <div class="footer">

        <!--<span></span>-->
    </div>
    <div style="clean:both"></div>
</div>
<script>

    (function () {
        var input1 = document.getElementById('price');


        new KeyBoard(input1,"","{$channel}");

        input1.onclick = function () {
            new KeyBoard(input1,"","{$channel}");
        };
    })();
</script>
</body>
</html>